﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    var orderId = $.request("orderId"),
        siteId = $.request("siteId"),
        spaceId = $.request("spaceId"),
        res = ['<option selected value="车辆">车辆</option>' ],
        cars = ['<option selected value="桑塔纳9527">桑塔纳9527</option>'],
        drivers = ['<option selected value="江师傅">江师傅</option>'],
        trIndex = 1;

    $(function () {
        //$.ajax({
        //    url: "/OrderFlowManage/ClientOrder/GetDeptByUsersJson",
        //    data: {},
        //    dataType: "json",
        //    async: false,
        //    success: function (data) {
        //        var index = 0;
        //        for (var i in data) {

        //            if (index == 0) {
        //                depts.push('<option selected value="' + i + '">' + i + '</option>');
        //                var items = data[i];
        //                for (var j = 0; j < items.length; j++) {
        //                    var item = items[j];
        //                    users.push('<option value="' + item.F_UserId + '">' + item.F_UserName + '</option>');
        //                }
        //            } else {
        //                depts.push('<option value="' + i + '">' + i + '</option>');
        //            }
        //            index++;
        //        }
        //        //console.log(depts);
        //        //console.log(users)
        //    }
        //});
    });

    function btn_add() {
        var $previewCarGrid = $('#previewCarGrid');
        var html = '';
        html += '<tr id="tr' + trIndex + '">';
        html += '<td>';
        html += trIndex;
        html += '</td>';
        html += '<td>';
        html += '<select id="txt_res' + trIndex + '" name="txt_res' + trIndex + '" class="form-control txt_res">';
        html += res.join('');
        html += '</select>';
        html += '</td>';
        html += '<td>';
        html += '<select id="txt_cars' + trIndex + '" name="txt_cars' + trIndex + '" index="' + trIndex + '" class="form-control txt_cars">';
        html += cars.join('');
        html += '</select>';
        html += '<td>';
        html += '<select id="txt_drivers' + trIndex + '" name="txt_drivers' + trIndex + '" class="form-control txt_drivers">';
        html += drivers.join('');
        html += '</select>';
        html += '</td>';
        html += '<td>';
        html += '<button type="button" class="btn btn-link btn-sm" onclick="btn_del(\'' + trIndex + '\')">删除</button>';
        html += '</td>';
        html += '</tr>';

        $previewCarGrid.append(html);
        $('#txt_res' + trIndex + ', #txt_cars' + trIndex + ', #txt_drivers' + trIndex).bindSelect();
        trIndex++;

    }

    function btn_del(index) {
        $('#tr' + index).remove();
    }

    function submitForm() {
        var $previewCarGrid = $('#previewCarGrid');
        var trs = $previewCarGrid.find('tr');

        if (trs.length) {
            var data = [];
            $(".txt_cars").each(function (i, ele) {
                var index = $(ele).attr("index");
                data.push(JSON.stringify({ resName: $('#txt_res' + index).val(), driverName: $('#txt_drivers' + index).val(), carId: ele.value, carName: $(ele).find(":selected").text() }));
            });
            var param = { OrderId: orderId, SiteId: siteId, SpaceId: spaceId, Data: data };
            // console.log(param);
            $.submitForm({
                url: "/OrderFlowManage/ClientOrder/SubmitPreviewCarForm?keyValue=" + orderId,
                param: param,
                success: function () {
                    $.currentWindow().vm.takelook.initTakelook();
                    $.loading(false);
                }
            });
        } else {
            $.modalMsg('请添加相关的陪同人员', 'success');
        }
    }
</script>

<form id="form1">
    <div style="margin:0 20px;">
        <table class="form">
            <tr>
                <th class="formTitle"><h4>预约车辆</h4></th>
                <td class="formValue">
                    <button class="btn btn-primary btn-sm" type="button" onclick="btn_add()">添加</button>
                </td>
            </tr>
        </table>
        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <td width="30">序号</td>
                    <td>资源</td>
                    <td width="100">车辆信息信息</td>
                    <td width="50">司机</td>
                    <td width="50">操作</td>
                </tr>
            </thead>
            <tbody id="previewCarGrid"></tbody>
        </table>
    </div>
</form>


